package com.controller;

import com.mapper.MenuMapper;
import com.service.account.AccountService;
import com.service.core.CoreService;
import com.service.template.Template;
import com.thread.AccessTokenThread;
import com.util.SignUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("SpringJavaAutowiringInspection")
@RestController
@RequestMapping("")
public class CoreController {
    @Autowired
    private CoreService coreService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private MenuMapper menuMapper;
    //验证是否来自微信服务器的消息
    @RequestMapping(value = "/{accountid}",method = RequestMethod.GET,produces = "text/plain;charset=utf-8")
    public String checkSignature(@PathVariable String accountid,@RequestParam(name = "signature" ,required = false) String signature  ,
                              @RequestParam(name = "timestamp" ,required = false) String timestamp  ,
                              @RequestParam(name = "nonce",required = false) String  nonce ,
                              @RequestParam(name = "echostr",required = false) String  echostr){
        if (SignUtil.checkSignature(signature, timestamp, nonce)) {
            accountService.findById(accountid);
            AccessTokenThread.getAccountid(accountid);
            Thread thread=new AccessTokenThread();
            thread.start();
            return echostr;
        }
        return "";
    }

    @RequestMapping(value = "/{accountid}",method = RequestMethod.POST, produces = "application/xml; charset=UTF-8")
    public  String post(@PathVariable String accountid,HttpServletRequest req){
        // 调用核心业务类接收消息、处理消息跟推送消息

        String respMessage = coreService.processRequest(req);
        return respMessage;
    }

    @RequestMapping(value = "/gettoken",method = RequestMethod.GET)
    public String getToken(){
        return AccessTokenThread.accessToken.getToken();
    }

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public void test(){
        Template.send_template_message();
        }


}
